/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cashmate.hibernate.dao;

import com.cashmate.hibernate.pojo.Borrowerinfo;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;

/**
 *
 * @author Lee
 */
public class BorrowerinfoDAO extends DAO {
    public BorrowerinfoDAO(){
        super();
    }
    
     //BASIC DAO METHODS
    @SuppressWarnings("unchecked")
    public List<Borrowerinfo> getAll() throws Exception {
        List<Borrowerinfo> a = (List<Borrowerinfo>) getSession().createQuery("from Borrowerinfo a").setCacheable(true).list();
        flush();
        return a;
    }

    @SuppressWarnings("unchecked")
    public Borrowerinfo get(int id) throws Exception {
        Borrowerinfo a = (Borrowerinfo) getSession().createQuery("from Borrowerinfo a where a.infoid=:id").setParameter("id", id).setCacheable(true).uniqueResult();
        flush();
        return a;
    }

    @SuppressWarnings("unchecked")
    public void update(Borrowerinfo a) throws Exception {
        try {
            begin();
            getSession().update(a);
            commit();
            flush();
        } catch (Exception e) {
            rollback();
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
    public void insert(Borrowerinfo a) throws Exception {
        try {
            begin();
            getSession().save(a);
            commit();
            flush();
        } catch (Exception e) {
            rollback();
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
    public void delete(int id) throws Exception {
        try {
            begin();
            getSession().delete(get(id));
            commit();
            flush();
        } catch (Exception e) {
            rollback();
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
    public List<Borrowerinfo> findByCriteria(DetachedCriteria dc, int from, int size) throws Exception {
        Criteria criteria = dc.getExecutableCriteria(getSession());
        criteria.setFirstResult(from);
        criteria.setMaxResults(size);
        criteria.setCacheable(true);
        List<Borrowerinfo> list = criteria.list();
        return list;
    }

    @SuppressWarnings("unchecked")
    public int countByCriteria(DetachedCriteria dc) throws Exception {
        Criteria criteria = dc.getExecutableCriteria(getSession());
        criteria.setCacheable(true);
        criteria.setProjection(Projections.rowCount());
        int count = ((Integer) criteria.list().get(0)).intValue();
        flush();
        return count;
    }
    
    //Other METHODS
    @SuppressWarnings("unchecked")
    public Borrowerinfo getLatestBorrowerinfo(int borid) throws Exception{
        Borrowerinfo b = (Borrowerinfo) getSession().createQuery("from Borrowerinfo b where b.borrowers.borrowerid=:bid order by b.infoid desc")
                    .setParameter("bid", borid).setCacheable(true).setMaxResults(1).uniqueResult();
        return b;
    }
}
